package com.ruoyi.shopping.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.shopping.domain.CommodityType;
import com.ruoyi.shopping.domain.CommodityTypeRequest;
import com.ruoyi.shopping.service.ICommodityTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品类型管理Controller
 * 
 * @author fxy
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/shopping/CommodityType")
public class CommodityTypeController extends BaseController
{
    @Autowired
    private ICommodityTypeService commodityTypeService;

    /**
     * 查询商品类型管理列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:CommodityType:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommodityType commodityType)
    {
        startPage();
        List<CommodityType> list = commodityTypeService.selectCommodityTypeList(commodityType);
        return getDataTable(list);
    }

    /**
     * 导出商品类型管理列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:CommodityType:export')")
    @Log(title = "商品类型管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityType commodityType)
    {
        List<CommodityType> list = commodityTypeService.selectCommodityTypeList(commodityType);
        ExcelUtil<CommodityType> util = new ExcelUtil<CommodityType>(CommodityType.class);
        util.exportExcel(response, list, "商品类型管理数据");
    }



    /**
     * 获取商品类型管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:CommodityType:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(commodityTypeService.selectCommodityTypeById(id));
    }

    /**
     * 新增商品类型管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:CommodityType:add')")
    @Log(title = "商品类型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommodityType commodityType)
    {
        return toAjax(commodityTypeService.insertCommodityType(commodityType));
    }

    /**
     * 修改商品类型管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:CommodityType:edit')")
    @Log(title = "商品类型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommodityType commodityType)
    {
        return toAjax(commodityTypeService.updateCommodityType(commodityType));
    }

    /**
     * 删除商品类型管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:CommodityType:remove')")
    @Log(title = "商品类型管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(commodityTypeService.deleteCommodityTypeByIds(ids));
    }

    /**
     * 批量添加商品类型管理
     * @param request
     * @return
     */
    @PreAuthorize("@ss.hasPermi('shopping:CommodityType:add')")
    @Log(title = "商品类型管理", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult addCommodityType(@RequestBody CommodityTypeRequest request) {
        try {
            commodityTypeService.addCommodityType(request);
            return AjaxResult.success("Categories added successfully");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("Internal server error");
        }
    }
    @PostMapping("/update")
    public ResponseEntity<?> add(@RequestBody CommodityTypeRequest request) {
        try {
            commodityTypeService.addCommodityType(request);
            return ResponseEntity.ok("提交成功");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("提交失败");
        }
    }
//    @PutMapping("/clear/{id}")
//    public ResponseEntity<String> clearSpecification(@PathVariable Long id) {
//        try {
//            commodityTypeService.clearSpecification(id);
//            return ResponseEntity.ok("Specification cleared successfully");
//        } catch (IllegalArgumentException e) {
//            return ResponseEntity.badRequest().body("Invalid ID");
//        } catch (Exception e) {
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error clearing specification");
//        }
//    }

    @PreAuthorize("@ss.hasPermi('shopping:CommodityType:edit')")
    @PutMapping("/clear/{id}")
    public AjaxResult clearSpecification(@PathVariable Long id)
    {
        return toAjax(commodityTypeService.clearSpecification(id));
    }

}
